package com.zis.auction.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zis.auction.dao.CarouselDao;
import com.zis.auction.model.Carousel;
import com.zis.auction.service.ICarouselInter;
import com.zis.base.impl.BaseInterImpl;
import com.zis.core.Constant;
import com.zis.core.ResponseEntity;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Create by wangshen 2018/1/3
 */
@Service
@Transactional("txManager")
public class CarouselInterImpl extends BaseInterImpl<Carousel> implements ICarouselInter {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    private CarouselDao carouselDao;

    @Override
    public String insert(Carousel carousel) throws Exception {
        carousel.setCreateDate(new Date());
        int num = sqlManager.insert(carousel);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String update(Carousel carousel) throws Exception {
        carousel.setUpdateDate(new Date());
        int num = sqlManager.updateTemplateById(carousel);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public String delete(String id) throws Exception {
        int num = sqlManager.deleteById(Carousel.class, id);
        if(num > 0){
            return ResponseEntity.createNormalJsonResponse(Constant.DATA_RESULT_SUCCESS);
        }
        return ResponseEntity.createErrorJsonResponse(Constant.DATA_RESULT_ERROR);
    }

    @Override
    public Carousel selectById(String id) throws Exception {
        return sqlManager.unique(Carousel.class, id);
    }

    @Override
    public List<Carousel> findByCondition(Carousel carousel) throws Exception {
        return null;
    }

    @Override
    public List<JSONObject> findThreeItem() {
        List<JSONObject> dataList = new ArrayList<>();
        List<Carousel> imgList = carouselDao.findThreeItem();
        if (imgList!=null && imgList.size()>0) {
            for (Carousel carousel : imgList) {
                JSONObject jsonObj = new JSONObject();
                jsonObj.put("img", carousel.getImgUrl());
                jsonObj.put("title", carousel.getImgIntro());
                dataList.add(jsonObj);
            }
        }
        return dataList;
    }
}
